//
//  CZTabBar.swift
//  sinaSwifWeiBo
//
//  Created by 张雪雪 on 16/2/24.
//  Copyright © 2016年 张雪雪. All rights reserved.
//

import UIKit

class CZTabBar: UITabBar {


    //重写init
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //来添加视图和约束
        
        setupUI()
    }
    //自定义了一个视图 会走init(frame: CGRect)
    //系统怕你采用storyboard ,会给你一个提示

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //2.创建一个私有方法来添加视图和设置约束
    
    private func setupUI(){
    
       addSubview(composeButton)
    
    
    }
    

    
    lazy var composeButton: UIButton = {
    
    let button = UIButton(type: UIButtonType.Custom)
        
        // 设置按钮的属性
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
    
        //可以添加点击事件
        
        //设置大小
        button.sizeToFit()
        
        return button

    
    
    }()
    
    //修改bar 的位置
    override func layoutSubviews() {
        
        
        //先调用super.layoutSubviews
        super.layoutSubviews()
        
        //创建一个索引
        var index = 0
        let width = bounds.width / 5
        let height = bounds.height

        for subview in subviews{
        
        
            //判断一下 subview 是不是 UITabBarButton
            //如果是 UITabBarButton 可以修改frame
            
            //string可以转类
            //NSClassFromString("UITabBarButton")返回了一个可选项
        
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
                //                print("UITabBarButton")
                //修改frame
              subview.frame = CGRectMake(CGFloat( index++ ) * width, 0, width, height)
            
                //判断一下索引
                
                if index == 2  {index++}
            
            
            }
        
        
        }
        //设置按钮的位置
        composeButton.frame = CGRectMake(2 * width, 0, width, height)
        //        print(subviews)
        //        print("=======")
 
        
    }
    
    
}
